package com.lianzai.mobile.controller;

import com.lianzai.car.domain.CarInfoDO;
import com.lianzai.car.service.CarInfoService;
import com.lianzai.common.constant.Constant;
import com.lianzai.common.utils.PageUtils;
import com.lianzai.common.utils.Query;
import com.lianzai.common.utils.QueryDO;
import com.lianzai.common.utils.R;
import com.lianzai.mobile.domain.CommunityInfoDO;
import com.lianzai.mobile.service.CommunityInfoService;
import com.lianzai.system.domain.UserDO;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.collections.map.HashedMap;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.List;
import java.util.Map;


// 车牌
@Slf4j
@Controller
public class PlateController {

    @Autowired
    private CommunityInfoService communityInfoService;

    @Autowired
    private CarInfoService carInfoService;


    @RequestMapping("/mobile/plate/add")
    public ModelAndView mobileIndex(Model model, HttpServletRequest request, HttpServletResponse response) {
        Query query = new Query();
        query.put("offset", 0);
        query.put("limit", 1000);
        query.put("status", 1);
        query.put("page", 1);

        List<CommunityInfoDO> communityInfos = communityInfoService.list(query);

        log.info("小区列表数量: {}" ,communityInfos.size());


        model.addAttribute("communityInfos", communityInfos);
        return new ModelAndView("wechat/addcar");
    }


    @RequestMapping("/mobile/plate/list")
    public ModelAndView mobilePlateList(Model model, HttpServletRequest request, HttpServletResponse response) {
        Long userId = (Long)request.getSession().getAttribute(Constant.CURR_USER_ID);
        Map<String, Object> params = new HashedMap();
        params.put("offset", 0);
        params.put("limit", 1000);
        params.put("userId", userId);
        Query query = new Query(params);
        List<CarInfoDO> cars = carInfoService.list(query);
        model.addAttribute("cars", cars);
        return new ModelAndView("wechat/cars");
    }


    /**
     * 保存车辆信息
     * @param request
     * @param carNo
     * @param communityId
     * @return
     */
    @RequestMapping(value = "/mobile/plate/saveCarInfo", method = RequestMethod.POST)
    @ResponseBody
    public R saveCarInfo(HttpServletRequest request,
                         @RequestParam("carNo") String carNo,
                         @RequestParam("communityId") Long communityId) {
        Long userId = (Long)request.getSession().getAttribute(Constant.CURR_USER_ID);
        CarInfoDO carInfoDO = new CarInfoDO();
        carInfoDO.setUserId(userId);
        carInfoDO.setCarNo(carNo);
        carInfoDO.setCommunityId(communityId);
        int save = carInfoService.save(carInfoDO);
        if(save <= 0){
            return R.error();
        }
        return R.ok();
    }


    /**
     * 查询小区列表
     * @param params
     * @return
     */
    @GetMapping("/mobile/plate/listCommunity")
    @ResponseBody
    public R listCommunity(@RequestParam Map<String, Object> params) {
        params.put("offset", 0);
        params.put("limit", 1000);
        params.put("status", 1);
        Query query = new Query(params);
        List<CommunityInfoDO> CommunityInfos = communityInfoService.list(query);
        return R.okObj(CommunityInfos);
    }

}
